package com.example.xui.Post_fragment;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.xui.Article_enter;
import com.example.xui.Comment_Article;
import com.example.xui.Main.FirstFragment;
import com.example.xui.Main.ThreeFragment;
import com.example.xui.R;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.yalantis.phoenix.PullToRefreshView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

public class recommend_fragment extends Fragment {
    RecyclerView mRecyclerView;
    MyAdapter mMyAdapter ;
    List<JSONObject> list1=new ArrayList<>();
    private PullToRefreshView mPullToRefreshView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_recommend_fragment, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        String sno=getActivity().getIntent().getStringExtra("sno");
        Log.i("tag12","sno="+sno);
        recommend_tie(sno);
        mRecyclerView = getActivity().findViewById(R.id.recyclerView2);
       /* mMyAdapter = new MyAdapter();
        mRecyclerView.setAdapter(mMyAdapter);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        mRecyclerView.setLayoutManager(layoutManager);*/
        mPullToRefreshView = (PullToRefreshView) getActivity().findViewById(R.id.pull_to_refresh);
        mPullToRefreshView.setOnRefreshListener(new PullToRefreshView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPullToRefreshView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mPullToRefreshView.setRefreshing(false);
                    }
                }, 2);
            }
        });
    }

    class MyAdapter extends RecyclerView.Adapter<MyViewHoder> {

        @NonNull
        @Override
        public MyViewHoder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = View.inflate(getContext(), R.layout.adapter_post, null);
            MyViewHoder myViewHoder = new MyViewHoder(view);
            return myViewHoder;
        }

        @Override
        public void onBindViewHolder(@NonNull MyViewHoder holder, int position) {
            try {
                holder.pictureto.setImageBitmap(BitmapFactory.decodeFile(list1.get(position).getString("invitationPicture")));
                holder.mTitleContent.setText(list1.get(position).getString("invitationContent"));
                holder.mTitleid.setText(list1.get(position).getString("invitationNickname"));
                holder.favorite.setText(list1.get(position).getString("invitationConmentNum"));
                holder.comment.setText(list1.get(position).getString("invitationConmentNum"));
                holder.post_enter.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent=new Intent(getContext(), Article_enter.class);
                        try {
                            intent.putExtra("id",list1.get(position).getString("invitationNickname"));
                            intent.putExtra("picture",list1.get(position).getString("invitationPicture"));
                            intent.putExtra("connect",list1.get(position).getString("invitationContent"));
                            Log.i("tag","connect="+list1.get(position).getString("invitationContent"));
                            intent.putExtra("resourceThumbNum",list1.get(position).getString("invitationThumbNum"));
                            intent.putExtra("resourceConmentNum",list1.get(position).getString("invitationConmentNum"));
                            intent.putExtra("resourceData",list1.get(position).getString("invitationData"));
                            intent.putExtra("image_connect",list1.get(position).getString("invitationPhoto"));
                            intent.putExtra("video_connect",list1.get(position).getString("invitationTv"));
                            startActivity(intent);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
              /*  holder.hear_view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent=new Intent(getContext(), Comment_Article.class);
                        startActivity(intent);
                    }
                });*/
            } catch (JSONException e) {
                e.printStackTrace();
            }
          /*  News news = mNewsList.get(position);
            holder.mTitleTv.setText(news.title);
            holder.mTitleContent.setText(news.content);*/
        }

        @Override
        public int getItemCount() {
            int a=0;
            //return list1.size();
            if(!list1.equals("[]")){
                a=list1.size();
            }
            return a;
        }
    }

    class MyViewHoder extends RecyclerView.ViewHolder {
        TextView mTitleid;
        TextView mTitleContent;
        ImageView pictureto;
        TextView favorite;
        TextView comment;
        ConstraintLayout post_enter;
        public MyViewHoder(@NonNull View itemView) {
            super(itemView);
            pictureto=itemView.findViewById(R.id.imageView26);
            mTitleid = itemView.findViewById(R.id.textView56);
            mTitleContent = itemView.findViewById(R.id.textView57);
            favorite=itemView.findViewById(R.id.textView58);
            comment=itemView.findViewById(R.id.textView59);
            post_enter=itemView.findViewById(R.id.post_data);
        }
    }

    public void recommend_tie(String i){
            new Thread(){
                @Override
                public void run() {
                    super.run();
                    RequestParams params = new RequestParams("http://10.138.87.49:8080/invitation/invitationData");
                    params.addHeader("Content-Type", "application/x-www-form-urlencoded");
                    x.http().post(params, new Callback.CacheCallback<JSONArray>() {
                        @Override
                        public void onSuccess(JSONArray result) {
                            //Log.i("sds",result);
                            List<JSONObject> list=new ArrayList<>();
                            for (int i = 0; i < result.length(); i++) {
                                try {
                                    JSONObject jsonObject = result.getJSONObject(i);
                                    list.add(jsonObject);
                                    //   Thread.sleep(2000);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }

                                getActivity().runOnUiThread(new Runnable() {
                                    @Override

                                    public void run() {
                                        list1 = list;
                                        mMyAdapter = new MyAdapter();
                                        mRecyclerView.setAdapter(mMyAdapter);
                                        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
                                        mRecyclerView.setLayoutManager(layoutManager);
                                      //  String son = getActivity().getIntent().getStringExtra("sno");
                                        try {
                                            Log.i("Tag", "tts=" + list1.get(0).getString("invitationUserID"));
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
// refresh ui 的操作代码             list1.a
                                    }

                                });
                            }
                        }

                        @Override
                        public void onError(Throwable ex, boolean isOnCallback) {
                            Log.i("ddsds","qweq");
                        }

                        @Override
                        public void onCancelled(CancelledException cex) {

                        }

                        @Override
                        public void onFinished() {

                        }

                        @Override
                        public boolean onCache(JSONArray result) {
                            return false;
                        }
                    });
                }
            }.start();
    }
}
